﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using CKFinder;

public partial class Admin_EditIntro : System.Web.UI.Page
{
    IntroductionController introductionController = new IntroductionController();
    protected void Page_Load(object sender, EventArgs e)
    {
        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor);
        if(!IsPostBack)
        {
            string id = Request.QueryString["id"];
            if(id==null || id=="" || string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {
                error();
            }
            int idd = Convert.ToInt32(id);
            DataTable dt = introductionController.GetByID(idd);
            Introductions inn = new Introductions(dt.Rows[0]);
            txtTitle.Text = inn.Title;
            txtUrl.Text = inn.Url;
            ckeditor.Text = inn.Content;
            if(inn.Status)
            {
                ddlStatus.SelectedIndex = 0;
            }
            else
            {
                ddlStatus.SelectedIndex = 1;
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Introductions intro = new Introductions();
        intro.Intro_id = Convert.ToInt32(Request.QueryString["id"]);
        intro.Title = txtTitle.Text;
        intro.Content = ckeditor.Text;
        intro.DateUpdate = DateTime.Now;
        intro.Url = txtUrl.Text;
        if(ddlStatus.SelectedIndex==0)
        {
            intro.Status = true;
        }
        else
        {
            intro.Status = false;
        }
        if(introductionController.Update(intro)>0)
        {
            Response.Write("<script>alert('Cập nhật thành công')</script>");
        }
        else
        {
            Response.Write("<script>alert('Cập nhật không thành công')</script>");
        }
    }
    private void error()
    {
        Response.Redirect("~/Error.aspx");
    }
}